package lesson13;

import lesson05.A;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public class Test01 {
    // 通过字节流读取文件
    public void testInputStream(){
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("1.txt");
            int i = 0;
            byte [] b = new byte[1024];
            while((i = fis.read(b)) != -1){
                System.out.print(new String(b,0,i));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    public void testRreader(){
        FileReader fr = null;
        try{
            fr = new FileReader("1.txt");
            BufferedReader br = new BufferedReader(fr);
            String str = null;
            while((str = br.readLine()) != null){
                System.out.println(str);
            }
//            int i = 0;
//            char [] c = new char[1024];
//            while((i = fr.read(c)) != -1){
//                System.out.println(new String(c));
//            }
        }catch(FileNotFoundException e){
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            if(fr != null){
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    // 字节输出流
    public void testOutputStream(){
        FileOutputStream fos = null;
        try {
            String str = "这是Java IO操作";
            fos = new FileOutputStream("2.txt");
            fos.write(str.getBytes());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    // 字符输出流
    public void testWriter(){
        FileWriter fw = null;
        try {
            fw = new FileWriter("2.txt");
            fw.write("你好，Java！");
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(fw != null){
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    // 对象序列化
    public void writeObject(Object obj){
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("object.dat");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(obj);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    // 对象反序列化
    public Object readObject(){
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("object.dat");
            ObjectInputStream ois = new ObjectInputStream(fis);
            return ois.readObject();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    public static void main(String[] args) {
        Test01 test01 = new Test01();
//        test01.testInputStream();
//        test01.testRreader();
//        test01.testOutputStream();
//        test01.testWriter();

//        Student stu = new Student("张三","男",20);
//        test01.writeObect(stu);
        ArrayList arrayList = new ArrayList();
//        arrayList.add(new Student("张三","男",20));
//        arrayList.add(new Student("李四","男",21));
//        arrayList.add(new Student("王五","男",22));
//        test01.writeObject(arrayList);
        List list = (List)test01.readObject();
        System.out.println(list);

    }
}
